'use strict';

const Controller = require('egg').Controller;

/**
* @controller ONUDetailsController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class ONUDetailsController extends Controller {
    /**
        * @summary WANProfile列表。
        * @description WANProfile列表。
        * @router get /wanProfile/VOIP
        * @request query String OltId
        * @request query String onuAddress
        */
     async getWanOrVoip(){
        let {ctx} = this;
        const params = ctx.query;
        let type = ctx.params.type;
        if(!params.onuAddress){
            return ctx.body = {
                msg:'onuAddress is required',
                status:400
            }
        }
        if(type!=="WANIP"&&type!=="VOIP"){
            return ctx.body = {
                msg:'type is error',
                status:400
            }
        }
        let oltResult = await this.ctx.service.common.getOltInfo(params.OltId);
        let result = await this.ctx.service.wanProfile.getWANLists(oltResult,type,params.onuAddress);
        ctx.body = {
            msg:'success',
            status:200,
            count:result.length,
            data:result
        }
    }
    validWanParams(params){
        if(params.mode==='dhcp'){
            if(params.wanIndex&&params.vlanProfile&&params.hostID){
                return {
                    flag:true
                }
            }else{
                return {
                    msg:'dhcp mode,wanIndex vlanProfile hostId is required',
                    flag:false,
                }
            }
        }else if(params.mode==='pppoe'){
            if(params.wanIndex&&params.pppoePassword&&params.pppoeUsername&&params.vlanProfile&&params.hostID){
                return {
                    flag:true
                }
            }else{
                return {
                    msg:'pppoe mode,wanIndex pppoePassword pppoeUsername vlanProfile hostID is required',
                    flag:false,
                }
            }

        }else if(params.mode==='static'){
            if(params.wanIndex&&params.ipProfile&&params.ipAddress&&params.mask&&params.hostID){
                return {
                    flag:true
                }
            }else{
                return {
                    msg:'dhcp mode,wanIndex ipProfile ipAddress mask hostId is required',
                    flag:false,
                }
            }
        }else{
            ctx.body = {
                msg:'mode match false',
                flag:false,
            }
        }
    }

    async createOrUpdateWan(){
        /**
        * @summary wanProfile添加/更新。
        * @description wanProfile添加/更新。
        * @router post /wanProfile
        * @request body wanProfileInfo value 传入参数
        */
        try {
            let {ctx} = this;
            let {wanIndex,mode,onuAddress,vlanProfile,hostID,pppoeUsername,pppoePassword,ipProfile,ipAddress,mask,OltId} = ctx.request.body;
            let params = {wanIndex,mode,onuAddress,vlanProfile,hostID,pppoeUsername,pppoePassword,ipProfile,ipAddress,mask}
            let res = this.validWanParams(params);
            if(res.flag){
                let oltResult = await this.ctx.service.common.getOltInfo(OltId);
                //发送命令
                let result = await ctx.service.wanProfile.createOrUpdateWan(oltResult,params);
                if(result.flag){
                    ctx.body = {
                        msg:'success',
                        status:200
                    }
                }else{
                    ctx.body = {
                        msg:result.message,
                        status:200
                    }
                }
            }else{
                ctx.body = {
                    msg:res.msg,
                    status:400,
                }
            }
        } catch (error) {
            throw error
        }
        
    }
    
    /**
     * 这个方法用于删除wan
     */
    async deteleWAN(){
        /**
        * @summary wanProfile删除。
        * @description wanProfile删除。
        * @router delete /wanProfile
        * @request body delWAN value 传入参数
        */
        let {ctx} = this;
        let {wanIndex,onuAddress,OltId} = ctx.request.body;
        let oltResult = await this.ctx.service.common.getOltInfo(OltId);
        //发送命令
        let result = await ctx.service.wanProfile.deleteWan(oltResult,{wanIndex,onuAddress});
        if(result){
            ctx.body = {
                msg:'success',
                status:200
            }
        }else{
            ctx.body = {
                msg:'fail',
                status:200
            }
        }
    }
    validVOIPParams(params){
        if(params.mode==='dhcp'){
            if(params.vlanProfile&&params.hostID){
                
            }else{
                return {
                    msg:'dhcp mode, vlanProfile hostId is required',
                    flag:false,
                }
            }
        }else if(params.mode==='pppoe'){
            if(params.pppoePassword&&params.pppoeUsername&&params.vlanProfile&&params.hostID){
                
            }else{
                return {
                    msg:'pppoe mode, pppoePassword pppoeUsername vlanProfile hostID is required',
                    flag:false,
                }
            }

        }else if(params.mode==='static'){
            if(params.ipProfile&&params.ipAddress&&params.mask&&params.hostID&&params.vlanProfile){
                
            }else{
                return {
                    msg:'dhcp mode, ipProfile ipAddress mask hostId is required',
                    flag:false,
                }
            }
        }else{
            return {
                msg:'mode match false',
                flag:false,
            }
        }
        if(params.onuAddress&&params.pots&&params.sipProfile&&params.userID&&params.sipUsername&&params.sipPassword){
            
        }else{
            return {
                msg:'onuAddress/pots/sipProfile/userID/sipUsername/sipPassword match false',
                flag:false,
            }
        }
        return {
            flag:true
        }
    }
    async createOrUpdateVOIP(){
        /**
        * @summary voip添加/更新。
        * @description voip添加/更新。
        * @router post /voip
        * @request body voipProfile value 传入参数
        */
        try {
            let {ctx} = this;
            let {mode,onuAddress,vlanProfile,hostID,pppoeUsername,pppoePassword,ipProfile,ipAddress,mask,OltId,type,pots,sipProfile,userID,sipUsername,sipPassword} = ctx.request.body;
            console.log(type);
            let params = {mode,onuAddress,vlanProfile,hostID,pppoeUsername,pppoePassword,ipProfile,ipAddress,mask,pots,sipProfile,userID,sipUsername,sipPassword}
            let res = this.validVOIPParams(params);
            if(res.flag){
                let oltResult = await this.ctx.service.common.getOltInfo(OltId);
                //发送命令
                let result = await ctx.service.wanProfile.createOrUpdateVOIP(oltResult,params);
                if(result.flag){
                    ctx.body = {
                        msg:'success',
                        status:200
                    }
                }else{
                    ctx.body = {
                        msg:result.message,
                        status:200
                    }
                }
            }else{
                ctx.body = {
                    msg:res.msg,
                    status:400,
                }
            }
        } catch (error) {
            throw error
        }
    }
    async deleteVoip(){
        /**
        * @summary voip删除。
        * @description voip删除。
        * @router delete /deleteVoip
        * @request body delVOIP value 传入参数
        */
         let {ctx} = this;
         let {onuAddress,pots,OltId} = ctx.request.body;
         let oltResult = await this.ctx.service.common.getOltInfo(OltId);
         //发送命令
         let result = await ctx.service.wanProfile.deleteVoip(oltResult,{onuAddress,pots});
         if(result){
             ctx.body = {
                 msg:'success',
                 status:200
             }
         }else{
             ctx.body = {
                 msg:'fail',
                 status:200
             }
         }
    }
    /**
    * @summary 获取onu的eth接口信息。
    * @description 获取onu的eth接口信息
    * @router get /getEth
    * @request query String OltId
    * @request query String onuAddress
    */
    async getEth(){
        let {ctx} = this;
        const params = ctx.query;
        let {OltId,onuAddress} = params;
        let oltInformation = await this.service.common.getOltInfo(OltId);
        let result = await this.service.wanProfile.getEth(oltInformation,onuAddress);
        ctx.body = {
            msg:'success',
            status:201,
            count:result.length,
            data:result
        }
    }

    /**
        * @summary 获取onu的catv信息。
        * @description 获取onu的catv信息。
        * @router get /getCATV
        * @request query String OltId
        * @request query String onuAddress
        */
    async getCATV(){
        let {ctx} = this;
        const params = ctx.query;
        let {OltId,onuAddress} = params;
        let oltInformation = await this.service.common.getOltInfo(OltId);
        let result = await this.service.wanProfile.getCATV(oltInformation,onuAddress);
        ctx.body = {
            msg:'success',
            status:201,
            count:result.length,
            data:result
        }
    }
    /** 
    * @summary 更改eth状态
    * @description 更改eth状态
    * @router POST /EthState
    * @request body updateETH value 传入参数
    */
    async updateEthState(){
        let {ctx} = this;
        let {OltId,onuAddress,ethInterface,ethStatus} = ctx.request.body;
        let oltInformation = await this.service.common.getOltInfo(OltId);
        let result = await this.service.wanProfile.updateEthState(oltInformation,{onuAddress,ethInterface,ethStatus});
        ctx.body = {
            msg:'success',
            status:201,
            count:result.length,
            data:result
        }
    }
}

module.exports = ONUDetailsController;
